﻿using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Abs.Core.DAL;
using Abs.Core.DAL.EntityFramework;
using Abs.Core.Models.User;
using NUnit.Framework;

namespace Abs.Core.UnitTesting
{
    //[TestFixture]
    public class SetUpDatabaseTest
    {
        private string connectionString;
        private AbsContext absContext;
        private SqlConnection sqlConnection;
        private SqlCommand sqlCommand;
        private SqlDataReader sqlDataReader;

        //[TestFixtureSetUp]
        public void BeforeTests()
        {
            ContextFactory.SetUpDatabase();
            this.connectionString = DbConfig.ConnectionString;
        }//end of public void BeforeTests()

        //[SetUp]
        public void BeforeEachTest()
        {
            this.absContext = ContextFactory.AbsContext;
            this.sqlConnection = new SqlConnection(this.connectionString);
        }//end of public void BeforeEachTest()

        //[TearDown]
        public void AfterEachTest()
        {
            if(this.absContext != null)
                this.absContext.Dispose();

            if(this.sqlConnection != null)
                this.sqlConnection.Dispose();

            if(this.sqlCommand != null)
                this.sqlCommand.Dispose();

            if(this.sqlDataReader != null)
                this.sqlDataReader.Dispose();
        }//public void AfterEachTest()

        //[Test]
        public void CountUsersByEf()
        {
            var countUsers = this.absContext.Users.Select(user => user).Count();
            var expectedResult = 10;
            Assert.AreEqual(expectedResult, countUsers);
        }//end of public void CountUsersByEf()

        //[Test]
        public void CountUsersByEfWithStoredProcedure()
        {
            var storedProcedure = "EXEC dbo.GetUsers";
            var users = this.absContext.Database.SqlQuery<User>(storedProcedure);
            var countUsers = users.Select(user => user).Count();
            var expectedResult = 10;
            Assert.AreEqual(expectedResult, countUsers);
        }//end of public void CountUsersByEfWithStoredProcedure()

        //[Test]
        public void CountUsersByAdoNetWithStoredProcedure()
        {
            var storedProcedure = "dbo.GetUsers";
            var countUsers = 0;
            var expectedResults = 10;

            this.sqlCommand = new SqlCommand(storedProcedure);
            this.sqlCommand.CommandType = CommandType.StoredProcedure;
            this.sqlConnection.Open();
            this.sqlCommand.Connection = this.sqlConnection;
            this.sqlDataReader = this.sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);

            while (sqlDataReader.Read())
                countUsers++;

            Assert.AreEqual(expectedResults, countUsers);
        }//end of public void CountUsersByAdoNetWithStoredProcedure()

        //[Test]
        public void SetConnectionString()
        {
            Assert.IsNotNullOrEmpty(DbConfig.ConnectionString);
        }//end of public void SetConnectionString()
    }//end of public class SetUpDatabaseTest
}//end of namespace ABS.UnitTesting